﻿namespace Sai.Core.Rules
{
	using Sai.Core.Introspection;

	/// <summary>
	/// Look for classes changing to interfaces, interfaces changing to classes.
	/// </summary>
	public class CheckTypeTaxonomy : IClassRule, IInterfaceRule, IEnumRule
	{
        public void Validate(ClassDeclaration was, ClassDeclaration isNow, ITypeDifference typeDifference)
        {
            CheckTaxonomy(was, isNow, typeDifference);
        }

        public void Validate(InterfaceDeclaration was, InterfaceDeclaration isNow, ITypeDifference typeDifference)
        {
            CheckTaxonomy(was, isNow, typeDifference);
        }

        public void Validate(EnumTypeDeclaration was, EnumTypeDeclaration isNow, ITypeDifference typeDifference)
        {
            CheckTaxonomy(was, isNow, typeDifference);
        }

        private void CheckTaxonomy(ITypeDeclaration was, ITypeDeclaration isNow, ITypeDifference typeDifference)
        {
            if (!was.MatchesType(isNow))
            {
                typeDifference.IdentifiedChange(
                    new UnaryTypeChange(
                        TypeChangeReason.TypeTaxonomy,
                        isNow.TypeName,
                        string.Format("Type was {0} is now {1}",
                            was.Taxonomy,
                            isNow.Taxonomy)
                    ));
            }
        }
    }
}
